package com.yima.baseproj.module.app.lunbotu;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yima.baseproj.module.app.lunbotu.model.AppLunbotuAddOrEditParamModel;
import com.yima.baseproj.module.app.lunbotu.model.AppLunbotuSearchModel;
import com.yima.baseproj.config.exception.type.ApiException;
import com.yima.baseproj.module.config.BaseController;
import com.yima.baseproj.module.config.utils.PageUtil;
import com.yima.baseproj.module.config.utils.PageVo;
import com.yima.baseproj.module.config.utils.ResultUtil;
import com.yima.baseproj.module.config.utils.ResultVo;
import com.yima.baseproj.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author 一码
 * @createDate 2023/4/1 09:00
 * @contact 1424529270@qq.com
 */
@Api(tags = "轮播图管理")
@RestController
@RequestMapping("/api/app/lunbotu")
public class AppLunbotuController extends BaseController {

    @Resource
    private AppLunbotuService appLunbotuService;

    @ApiOperation("查询分页")
    @GetMapping(value = "/findAllByPage")
    public Object findAllByPage(PageVo page, AppLunbotuSearchModel searchVo) {
        Page<Map> pageData = appLunbotuService.findByPage(PageUtil.initMpPage(page),searchVo);
        return pageData;
    }

    @ApiOperation("查询列表")
    @GetMapping(value = "/findAllByList")
    public ResultVo findAllByList(AppLunbotuSearchModel searchVo) {
        String userId = SecurityUtil.getUserId();
        searchVo.setUserId(userId);
        List<Map> list = appLunbotuService.findByList(searchVo);
        return ResultUtil.success(list);
    }

    @ApiOperation("添加")
    @PostMapping(value = "/add")
    public void add(@Validated @RequestBody AppLunbotuAddOrEditParamModel paramModel) {
        AppLunbotuEntity appLunbotuEntity = new AppLunbotuEntity();
        appLunbotuEntity.setData(paramModel);
        appLunbotuService.save(appLunbotuEntity);
    }

    @ApiOperation("修改")
    @PostMapping(value = "/edit")
    public void edit(@Validated @RequestBody AppLunbotuAddOrEditParamModel paramModel) {
        if(StringUtils.isBlank(paramModel.getId())){
            throw  new ApiException("数据不存在，请刷新重试");
        }
        AppLunbotuEntity appLunbotuEntity = appLunbotuService.getById(paramModel.getId());
        if(appLunbotuEntity == null){
            throw  new ApiException("数据不存在，请刷新重试");
        }

        appLunbotuEntity.setData(paramModel);
        appLunbotuService.saveOrUpdate(appLunbotuEntity);
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/delByIds/{ids}")
    public void delByIds(@PathVariable String[] ids) {
        appLunbotuService.removeBatchByIds(Arrays.asList(ids));
    }
}